package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.common.Log;
import com.migo.defence.RtCode;
import com.migo.entity.AppDownloadEntity;
import com.migo.entity.MgSampleVideoEntity;
import com.migo.entity.PvMgFirmwareVerisonEntity;
import com.migo.enums.BusinessType;
import com.migo.mongo.entity.ReleaseNoteMongo;
import com.migo.mongo.service.ReleaseNoteMongoService;
import com.migo.oss.OSSFactory;
import com.migo.service.AppDownloadService;
import com.migo.service.SysOssService;
import com.migo.utils.MGUtils;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import com.migo.view.AppDownLoadView;
import com.migo.view.MgFirmwareView;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api")
public class ApiAppDownloadController {

    @Autowired
    OSSFactory ossFactory;

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private AppDownloadService appDownloadService;


    @Autowired
    private ReleaseNoteMongoService releaseNoteMongoService;

    @Autowired
    private RedisCacheUtil<List<AppDownloadEntity>> redisCacheUtil;

    /**
     * 检查App的版本是否为最新
     */
    @IgnoreAuth
    @Log(title = "API检查APP版本", businessType = BusinessType.API)
    @RequestMapping(value = "checkAppVersion", produces = "application/json;charset=utf-8;")
    public R checkAppVersion(@RequestParam Map<String, String> params) {

        try {
            String app_version = params.get("app_version");
            String mobile_system = params.get("mobile_system");
            //String language = params.get("language");
            Map<String, Object> queryParams = new HashMap<String, Object>();
            //查询最新的版本信息 status=0
            queryParams.put("status", 0);
            queryParams.put("mobileSystem", mobile_system);
            List<AppDownloadEntity> appDownloadList = null;
            //缓存部分 开始
            String key = "checkAppVersion_" + mobile_system;
            if (redisCacheUtil.hashKey(key)) {
                appDownloadList = redisCacheUtil.getCacheList(key);
                if (params.containsKey("flush")) {  //刷新标志
                    redisCacheUtil.deleteObject(key);
                }
            } else {
                //查询数据库写入redis
                synchronized (this) {
                    appDownloadList = appDownloadService.queryListByConditions(queryParams);
                    redisCacheUtil.setCacheList(key, appDownloadList);
                }
            }
            //缓存部分 结束
            String isUpdate = "0";
            AppDownLoadView appDownLoadView = new AppDownLoadView();
            appDownLoadView.setAppDownloadList(appDownloadList);
            if (appDownloadList != null) {
                /*String appVersionName = appDownloadList.get(0).getAppVersionName();
                String LatestVersionCode = appDownloadList.get(0).getAppLatestVersionCode();*/
                String lastAppVersion = appDownloadList.get(0).getAppVersionName();
                if (lastAppVersion.contains("v") || lastAppVersion.contains("V")) {
                    lastAppVersion = lastAppVersion.replace("v", "");
                    lastAppVersion = lastAppVersion.replace("V", "");
                }
                if (app_version.contains("v") || app_version.contains("V")) {
                    app_version = app_version.replace("v", "");
                    app_version = app_version.replace("V", "");
                }
                //版本号一致
                if (MGUtils.compareVersion(app_version, lastAppVersion) == 0 || MGUtils.compareVersion(app_version, lastAppVersion) == 1) {
                    appDownLoadView.setIsUpdate("0");
                    appDownLoadView.setShowMessage("已经是最新版本");
                } else {
                    appDownLoadView.setIsUpdate("1");
                    appDownLoadView.setShowMessage("发现最新版本，是否安装？");
                }
                ;
            }

            return R.ok().put("data", appDownLoadView);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }

    /**
     * App 下载
     */

    @IgnoreAuth
    @RequestMapping(value = "appDownLoad", produces = "application/json;charset=utf-8;")
    public R appDownLoad(@RequestParam Map<String, String> params) {

        Map<String, Object> queryParams = new HashMap<String, Object>();
        //查询最新的版本信息 status=0
        queryParams.put("status", 0);
        List<AppDownloadEntity> appDownloadList = appDownloadService.queryListByConditions(queryParams);
        return R.ok().put("data", appDownloadList);
    }


    /**
     * App release note
     */
    @IgnoreAuth
    @RequestMapping(value = "queryReleaseNote", produces = "application/json;charset=utf-8;")
    public R queryReleaseNote(@RequestParam Map<String, String> params) {
        Query query = Query.query(new Criteria());
        List<ReleaseNoteMongo> releaseNoteMongoList = releaseNoteMongoService.list(query);
        return R.ok().put("data", releaseNoteMongoList);
    }


}
